home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / TURBOPASCAL WIN / DOCDEMOS.PAK / DLLTEST.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-08  |  5KB  |  208 lines

  1. {*****************************************************}
  2. {                                                     }
  3. {  Turbo Pascal for Windows                           }
  4. {  Demo program                                       }
  5. {  Copyright (c) 1991, 1992 by Borland International  }
  6. {                                                     }
  7. {*****************************************************}
  8.  
  9.  
  10. {  This program demonstrates how to use functions located in a DLL.  The unit
  11.    MATH.PAS provides the function names and parameters of the math functions
  12.    this program uses.
  13.  
  14.    *NOTE*  You need to compile MATHDLL.PAS to produce the DLL module this
  15.    program uses BEFORE you try to run this program.
  16.  
  17. }
  18.  
  19.  
  20.  
  21.  
  22. program DLLTest;
  23.  
  24. {$R MATH}
  25.  
  26. uses WinTypes, WinProcs, WObjects, Math;
  27.  
  28. type
  29.   TMyApplication = object(TApplication)
  30.     procedure InitMainWindow; virtual;
  31.   end;
  32.  
  33.   PFinancialDialog = ^TFinancialDialog;
  34.   TFinancialDialog = object(TDialog)
  35.     Error: Word;
  36.     Period, Interest, Term, Principal, Payment : Real;
  37.     function LoadFields: Boolean;  virtual;
  38.     procedure OK(var Msg: TMessage); virtual id_First + IdOk;
  39.     function Calculate: Real; virtual;
  40.   end;
  41.  
  42.   PPaymentDialog = ^TPaymentDialog;
  43.   TPaymentDialog = object(TFinancialDialog)
  44.     function LoadFields: Boolean; virtual;
  45.     function Calculate: Real; virtual;
  46.   end;
  47.  
  48.   PPrincipalDialog = ^TPrincipalDialog;
  49.   TPrincipalDialog = object(TFinancialDialog)
  50.     function LoadFields: Boolean; virtual;
  51.     function Calculate: Real; virtual;
  52.   end;
  53.  
  54.  
  55.   PMyWindow = ^TMyWindow;
  56.   TMyWindow = Object(TWindow)
  57.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  58.     procedure Payment(var Msg: TMessage); virtual cm_First + cm_Payment;
  59.     procedure Principal(var Msg: TMessage); virtual cm_First + cm_Principal;
  60.   end;
  61.  
  62. function TFinancialDialog.LoadFields: Boolean;
  63. var
  64.   E: Integer;
  65.   S: array[0..50] of char;
  66. begin
  67.   LoadFields := False;
  68.   Error := 0;
  69.  
  70.   GetDlgItemText(HWindow, id_Interest, S, SizeOf(S));
  71.   Val(S, Interest, E);
  72.   if E<>0 then
  73.     Error := er_MustHaveValue
  74.   else
  75.     if (Interest > 1) then  
  76.       Error := er_InterestLimit;
  77.    
  78.   if (Error = 0) then
  79.   begin
  80.     GetDlgItemText(HWindow, id_Term, S, SizeOf(S));
  81.     Val(S, Term, E);
  82.     If E<>0 then
  83.       Error := er_MustHaveValue
  84.     else
  85.       if Term > 50 then
  86.         Error := er_TermLimit;
  87.   end;
  88.  
  89.   if (Error = 0) then
  90.   begin
  91.     GetDlgItemText(HWindow, id_Periods, S, SizeOf(S));
  92.     Val(S, Period, E);
  93.     If E<>0 then
  94.       Error := er_MustHaveValue;
  95.   end;
  96.  
  97.   LoadFields:= Error = 0;
  98. end;
  99.  
  100. procedure TFinancialDialog.Ok(var Msg: TMessage);
  101. var
  102.   S: array [0..100] of char;
  103. begin
  104.   if LoadFields then
  105.   begin
  106.     FillChar(S, SizeOf(S), #0);
  107.     Str(Calculate:10:2, S);
  108.     SetDlgItemText(HWindow, id_Output, S);
  109.   end
  110.   else
  111.   begin  { Load error string from the resource file }
  112.     LoadString(HInstance, Error, S, SizeOf(S));
  113.     WriteError(HWindow, S);
  114.   end;
  115. end;
  116.  
  117. function TFinancialDialog.Calculate: Real;
  118. begin
  119.   Abstract;
  120. end;
  121.  
  122.  
  123.  
  124. function TPaymentDialog.LoadFields: Boolean;
  125. var
  126.   E: Integer;
  127.   S: array [0..100] of char;
  128. begin
  129.   if TFinancialDialog.LoadFields then
  130.   begin
  131.     GetDlgItemText(HWindow, id_Principal, S, SizeOf(S));
  132.     Val(S, Principal, E);
  133.     If E<>0 then
  134.       Error := er_MustHaveValue
  135.     else
  136.       if Principal > 1000000 then
  137.         Error := er_PrincipalLimit;
  138.     LoadFields := Error = 0;
  139.   end
  140.   else
  141.     LoadFields := False;
  142. end;
  143.  
  144.  
  145. function TPaymentDialog.Calculate: Real;
  146. begin
  147.   Payment := Payments(Period, Interest, Term, Principal);
  148.   Calculate := Payment;
  149. end;
  150.  
  151.  
  152. function TPrincipalDialog.LoadFields: Boolean;
  153. var
  154.   E: Integer;
  155.   S: array [0..100] of char;
  156. begin
  157.   if TFinancialDialog.LoadFields then
  158.   begin
  159.     GetDlgItemText(HWindow, id_Payment, S, SizeOf(S));
  160.     Val(S, Payment, E);
  161.     If E<>0 then
  162.       Error := er_MustHaveValue
  163.     else
  164.       if Payment < 1 then
  165.         Error := er_PaymentLimit;
  166.     LoadFields := Error = 0;
  167.   end
  168.   else
  169.     LoadFields := False;
  170. end;
  171.  
  172.  
  173. function TPrincipalDialog.Calculate: Real;
  174. begin
  175.   Principal := Principals(Payment, Period, Interest, Term);
  176.   Calculate := Principal;
  177. end;
  178.  
  179.  
  180. constructor TMyWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  181. begin
  182.   TWindow.Init(AParent, ATitle);
  183.   Attr.Menu := LoadMenu(HInstance, 'Menu');
  184. end;
  185.  
  186. procedure TMyWindow.Payment(var Msg: TMessage);
  187. begin
  188.   Application^.ExecDialog(new(PPaymentDialog, Init(@Self, 'PaymentDlg')));
  189. end;
  190.  
  191. procedure TMyWindow.Principal(var Msg: TMessage);
  192. begin
  193.   Application^.ExecDialog(new(PPrincipalDialog, Init(@Self, 'PrincipalDlg')));
  194. end;
  195.  
  196. procedure TMyApplication.InitMainWindow;
  197. begin
  198.   MainWindow:=New(PMyWindow, Init(nil, 'DLL Test'));
  199. end;
  200.  
  201. var
  202.   MyApplication: TMyApplication;
  203. begin
  204.   MyApplication.Init('DLLTest');
  205.   MyApplication.Run;
  206.   MyApplication.Done;
  207. end.
  208.